package com.egao.trade.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;

import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.trade.biz.TradeGoodsBiz;
import com.egao.trade.entity.TradeGoods;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2022-04-18 15:49:37
 * @version ：1.0
 */
@Repository
public class TradeGoodsBizImpl extends BaseDao implements TradeGoodsBiz {

	@Override
	public JsonResult list(PageResult2<T> page, TradeGoods tradeGoods, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<TradeGoods> rm = new BeanPropertyRowMapper<TradeGoods>(TradeGoods.class);
		StringBuffer sql = new StringBuffer("select t.id,t.organization_id,t.type1");
    sql.append(",t.type2,t.pic_url,t.describes,t.ref_price");
		sql.append(",t.least_order,t.payment,t.goods_size,t.limit_use");
		sql.append(",t.color,t.nums,t.price,t.freight");
		sql.append(",t.order_num,t.create_time,t.create_user,t.update_time");
		sql.append(",t.update_user");
    sql.append(",suser.nick_name createName ");
    sql.append(" from trade_goods t ");
    sql.append(" inner join sys_user suser on suser.user_id=t.create_user ");
    
    sql.append(" where t.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and t.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(tradeGoods)){
//			if(Utils.isNotEmpty(tradeGoods.getName())){
//				sql.append(" and instr(t.name,:name)>0 ");
//				map.put("name", tradeGoods.getName());
//			}
      if(Utils.isNotEmpty(tradeGoods.getSort())&&Utils.isNotEmpty(tradeGoods.getOrder())){
        sql.append(" order by t."+tradeGoods.getSort()+" "+tradeGoods.getOrder());
      } else {
        sql.append(" order by t.create_time desc");
      }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<TradeGoods> list(TradeGoods tradeGoods, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<TradeGoods> rm = new BeanPropertyRowMapper<TradeGoods>(TradeGoods.class);
    StringBuffer sql = new StringBuffer("select t.id,t.organization_id,t.type1");
    sql.append(",t.type2,t.pic_url,t.describes,t.ref_price");
		sql.append(",t.least_order,t.payment,t.goods_size,t.limit_use");
		sql.append(",t.color,t.nums,t.price,t.freight");
		sql.append(",t.order_num,t.create_time,t.create_user,t.update_time");
		sql.append(",t.update_user");
    sql.append(",suser.nick_name createName ");
    sql.append(" from trade_goods t ");
    sql.append(" inner join sys_user suser on suser.user_id=t.create_user ");
    
    sql.append(" where t.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and t.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(tradeGoods)){
      if(Utils.isNotEmpty(tradeGoods.getSort())&&Utils.isNotEmpty(tradeGoods.getOrder())){
        sql.append(" order by t."+tradeGoods.getSort()+" "+tradeGoods.getOrder());
      } else {
        sql.append(" order by t.create_time desc");
      }
    }
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public TradeGoods getById(TradeGoods tradeGoods) {
		RowMapper<TradeGoods> rm = new BeanPropertyRowMapper<TradeGoods>(TradeGoods.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select t.id,t.organization_id,t.type1");
    sql.append(",t.type2,t.pic_url,t.describes,t.ref_price");
		sql.append(",t.least_order,t.payment,t.goods_size,t.limit_use");
		sql.append(",t.color,t.nums,t.price,t.freight");
		sql.append(",t.order_num,t.create_time,t.create_user,t.update_time");
		sql.append(",t.update_user");
    sql.append(",suser.nick_name createName ");
    sql.append(" from trade_goods t ");
    sql.append(" inner join sys_user suser on suser.user_id=t.create_user ");
    
    sql.append(" where t.deleted=0 and t.id=:id ");
		map.put("id", tradeGoods.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(TradeGoods tradeGoods, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into trade_goods (");
    sql.append("organization_id,type1,type2,pic_url");
		sql.append(",describes,ref_price,least_order,payment");
		sql.append(",goods_size,limit_use,color,nums");
		sql.append(",price,freight,order_num,create_user,create_time) values ( ");
    sql.append(":organization_id,:type1,:type2,:pic_url");
		sql.append(",:describes,:ref_price,:least_order,:payment");
		sql.append(",:goods_size,:limit_use,:color,:nums");
		sql.append(",:price,:freight,:order_num,:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("type1", tradeGoods.getType1());
    map.put("type2", tradeGoods.getType2());
    map.put("pic_url", tradeGoods.getPicUrl());
    map.put("describes", tradeGoods.getDescribes());
    map.put("ref_price", tradeGoods.getRefPrice());
    map.put("least_order", tradeGoods.getLeastOrder());
    map.put("payment", tradeGoods.getPayment());
    map.put("goods_size", tradeGoods.getGoodsSize());
    map.put("limit_use", tradeGoods.getLimitUse());
    map.put("color", tradeGoods.getColor());
    map.put("nums", tradeGoods.getNums());
    map.put("price", tradeGoods.getPrice()==null?null:tradeGoods.getPrice()+"");
    map.put("freight", tradeGoods.getFreight()==null?null:tradeGoods.getFreight()+"");
    map.put("order_num", tradeGoods.getOrderNum()==null?null:tradeGoods.getOrderNum()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(TradeGoods tradeGoods, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update trade_goods set ");
    sql.append("type1=:type1,type2=:type2,pic_url=:pic_url ");
		sql.append(",describes=:describes,ref_price=:ref_price,least_order=:least_order ");
		sql.append(",payment=:payment,goods_size=:goods_size,limit_use=:limit_use ");
		sql.append(",color=:color,nums=:nums,price=:price ");
		sql.append(",freight=:freight,order_num=:order_num,update_time=now() ");
		sql.append(",update_user=:update_user where id=:id ");
    map.put("type1", tradeGoods.getType1());
    map.put("type2", tradeGoods.getType2());
    map.put("pic_url", tradeGoods.getPicUrl());
    map.put("describes", tradeGoods.getDescribes());
    map.put("ref_price", tradeGoods.getRefPrice());
    map.put("least_order", tradeGoods.getLeastOrder());
    map.put("payment", tradeGoods.getPayment());
    map.put("goods_size", tradeGoods.getGoodsSize());
    map.put("limit_use", tradeGoods.getLimitUse());
    map.put("color", tradeGoods.getColor());
    map.put("nums", tradeGoods.getNums());
    map.put("price", tradeGoods.getPrice()==null?null:tradeGoods.getPrice()+"");
    map.put("freight", tradeGoods.getFreight()==null?null:tradeGoods.getFreight()+"");
    map.put("order_num", tradeGoods.getOrderNum()==null?null:tradeGoods.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", tradeGoods.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(TradeGoods tradeGoods, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(tradeGoods.getId().length()>0){
			for(String ID : tradeGoods.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update trade_goods set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
